﻿using PerformanceExamine.Utility;
using PEUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PEModels
{
    public class Users
    {
        public int Id { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string Sex { get; set; }
        public int BaseTypeId { get; set; }
        public bool IsDel { get; set; }

        public static List<Users> GetListUsers()
        {
            List<Users> users = new List<Users>();
            DataTable dt = SqlHelper.ExecuteTable("select * from Users");
            foreach (DataRow dr in dt.Rows)
            {
                users.Add(dr.DataRowToModel<Users>( ));
            }
            return users;
        }
       
        public static int Insert(Users user)
        {
            return SqlHelper.ExecuteNonQuery($"INSERT INTO Users(UserName,Sex,Password,BaseTypeId,IsDel) VALUES  (@UserName,@Sex,@Password,@BaseTypeId,@IsDel)",
                new SqlParameter("@UserName", user.UserName),
                new SqlParameter("@Sex", user.Sex),
                new SqlParameter("@Password", user.Password),
                new SqlParameter("@BaseTypeId", user.BaseTypeId),
                new SqlParameter("@IsDel", user.IsDel)
                );
        }

        public static int Update(Users user)  
        {
            return SqlHelper.ExecuteNonQuery($"UPDATE Users SET UserName=@UserName,Sex=@Sex,Password=@Password,BaseTypeId=@BaseTypeId,IsDel=@IsDel WHERE Id=@Id",
                new SqlParameter("@Id", user.Id),
                new SqlParameter("@UserName", user.UserName),
                new SqlParameter("@Sex", user.Sex),
                new SqlParameter("@Password", user.Password),
                new SqlParameter("@BaseTypeId", user.BaseTypeId),
                new SqlParameter("@IsDel", user.IsDel)
                );
        }
    }
}
